<?php

namespace Admin\Controller;
use Think\Controller;

class WithdrawalsController extends Controller {

	//广告主添加提现
    public function advertisers_add(){
    	$price = I('get.name');
    	$ad_id = I('get.ad_id');
    	$M = M(); 
        if(!$ad_id){
            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "缺少广告主id";
            $this->ajaxReturn($ajaxReturn);
        }
        if(!$price){
            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "缺少提现金额";
            $this->ajaxReturn($ajaxReturn);
        }

    	$withdrawals = M('advertisers_withdrawals');
    	$sql ="SELECT b.name,a.cid,a.corporate_name,a.balance,a.id FROM table_advertisers as a INNER JOIN table_admin_user as b ON a.cid=b.id WHERE a.id=$ad_id LIMIT 1";
    	$user = $M->query($sql);
    	if($user[0]['balance']<$price){
    		$ajaxReturn['code'] = 2;
    		$ajaxReturn['msg'] = "账户余额不足";
    		$this->ajaxReturn($ajaxReturn);
    	}

    	foreach($user as $key=>$v){
    		$data['corporate_name'] = $v['corporate_name'];
    		$data['name'] = $v['name'];
    		$data['cid'] = $v['cid'];
    		$data['ad_id'] = $v['id'];
    		$data['time'] = time();
    		$data['money'] = $price;

    	}

    	$result = $withdrawals->add($data);
    	if($result){
    		$ajaxReturn['code'] = 1;
    		$ajaxReturn['msg'] = "SUCCESS";
    	}else{
    		$ajaxReturn['code'] = 2;
    		$ajaxReturn['msg'] = "提现申请失败";
    	}

    	$this->ajaxReturn($ajaxReturn);

    }

    //渠道商添加提现
    public function channel_add(){
    	$price = I('get.name');
    	$ch_id = I('get.ch_id');

        if(!$ch_id){
            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "缺少渠道商id";
            $this->ajaxReturn($ajaxReturn);
        }
        if(!$price){
            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "缺少提现金额";
            $this->ajaxReturn($ajaxReturn);
        }
    	$M = M();
    	$withdrawals = M('channel_withdrawals');
    	$sql ="SELECT b.name,a.mid,a.corporate_name,a.balance,a.id FROM table_channel as a INNER JOIN table_admin_user as b ON a.mid=b.id LIMIT 1";
    	$user = $M->query($sql);
    	if($user[0]['balance']<$price){
    		$ajaxReturn['code'] = 2;
    		$ajaxReturn['msg'] = "账户余额不足";
    		$this->ajaxReturn($ajaxReturn);
    	}
    	foreach($user as $key=>$v){
    		$data['corporate_name'] = $v['corporate_name'];
    		$data['name'] = $v['name'];
    		$data['mid'] = $v['mid'];
    		$data['ch_id'] = $v['id'];
    		$data['time'] = time();
    		$data['money'] = $price;
    	}

    	$result = $withdrawals->add($data);
    	if($result){
    		$ajaxReturn['code'] = 1;
    		$ajaxReturn['msg'] = "SUCCESS";
    	}else{
    		$ajaxReturn['code'] = 2;
    		$ajaxReturn['msg'] = "提现申请失败";
    	}
    	$this->ajaxReturn($ajaxReturn);
    }

    //广告商提现列表
    public function advertisers_list(){
        $page = I('get.page');
        $page_size = I('get.page_size');

    	$table = M('advertisers_withdrawals');

        $count = $table->count();
        $total =  ceil($count / $page_size);
        $limitStart = ($page-1) * $page_size;

        $res = $table->order('id desc')->limit($limitStart,$page_size)->where('state=0')->select();


    		$data['page']['total_page'] = $total;
            $data['list'] = $res;

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $data;
    	
    	$this->ajaxReturn($ajaxReturn);
    }

    //广告主提现搜索
    public function advertisers_search(){
        $page = I('post.page');
        $page_size = I('post.page_size');
        $start_time = I('post.start_time');
        $end_time = I('post.end_time');
        $sign = I('post.status');

        $table = M('advertisers_withdrawals');
        $where = "state=0";
        if($start_time){
            $where.=" and settlement_time>=$start_time";
        }
        if($end_time){
            $where.=" and settlement_time<=$end_time";
        }
        if($sign!=''){
            $where.=" and sign=$sign";
        }
        $count = $table->where($where)->count();
        $total =  ceil($count / $page_size);
        $limitStart = ($page-1) * $page_size;

        $res = $table->order('id desc')->limit($limitStart,$page_size)->where($where)->select();

      
            $data['page']['total_page'] = $total;
            $data['list'] = $res;
            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $data;
       
        $this->ajaxReturn($ajaxReturn);
    }

    //渠道商提现列表
    public function channel_list(){
        $page = I('post.page');
        $page_size = I('post.page_size');
    	$where['state'] = 0;
    	$table = M('channel_withdrawals');

        $count = $table->where($where)->count();
        $total =  ceil($count / $page_size);
        $limitStart = ($page-1) * $page_size;

        $res = $table->order('id desc')->limit($limitStart,$page_size)->where($where)->select();
    
    		$data['page']['total_page'] = $total;
            $data['list'] = $res;
            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $data;
    
    	$this->ajaxReturn($ajaxReturn);
    }

    //渠道商提现搜索
    public function channel_search(){
        $page       = I('post.page');
        $page_size  = I('post.page_size');
        $start_time = I('post.start_time');
        $end_time   = I('post.end_time');
        $sign       = I('post.status');
        $table = M('channel_withdrawals');
        $where="state=0";
        if($sign!=''){
            $where.=" and sign=$sign";
        }
        $count = $table->where($where)->count();
        $total =  ceil($count / $page_size);
        $limitStart = ($page-1) * $page_size;
        $res = $table->order('id desc')->limit($limitStart,$page_size)->where($where)->select();

       
            $data['page']['total_page'] = $total;
            $data['list'] = $res;
            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $data;
        
        $this->ajaxReturn($ajaxReturn);
    }

    //广告主修改提现状态 
    public function advertisers_state(){
        $name = session('user_info');
    	$id = I('get.id');
    	$state = I('get.type');
    	$name  = $name['uname'];
        $price = I('get.price');

        if(empty($id)){
            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "缺少修改当前条数id";
            $this->ajaxReturn($ajaxReturn);
        }
        if(empty($state)){
            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "缺少要修改的状态";
            $this->ajaxReturn($ajaxReturn);
        }
        if(empty($name)){
            $ajaxReturn['code'] = 3;
            $ajaxReturn['msg'] = "缺少审核人姓名";
            $this->ajaxReturn($ajaxReturn);
        }
        if(empty($price)){
            $ajaxReturn['code'] = 5;
            $ajaxReturn['msg'] = "缺少要修改的金额";
            $this->ajaxReturn($ajaxReturn);
        }

        $res = M('advertisers_withdrawals')->where("id=$id")->find();
        if($res['sign']==0){

            $data['sign'] = $state;
            $data['settlement_person'] = $name;
            $data['settlement_time'] = time();
            $advertisers = M('advertisers');

            $table = M('advertisers_withdrawals')->where("id=$id")->save($data);

            $res = $advertisers->where("id=".$res['ad_id'])->setDec("balance",$price);

            if($res){
                $ajaxReturn['code'] = 1;
                $ajaxReturn['msg'] = "操作成功！";
            }else{
                $ajaxReturn['code'] = 2;
                $ajaxReturn['msg'] = "操作失败！";
            }
        }else{
            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "已经审核！";
        }
        

    	$this->ajaxReturn($ajaxReturn);
    }

    //渠道商修改提现状态
  	public function channel_state(){
  		$name = session('user_info');
        $id = I('get.id');
        $state = I('get.type');
        $name  = $name['uname'];
        $ch_id = I('get.ch_id');
        $price = I('get.price');
        if(empty($id)){
            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "缺少修改当前条数id";
            $this->ajaxReturn($ajaxReturn);
        }
        if(empty($state)){
            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "缺少要修改的状态";
            $this->ajaxReturn($ajaxReturn);
        }
        if(empty($name)){
            $ajaxReturn['code'] = 6;
            $ajaxReturn['msg'] = "缺少审核人姓名";
            $this->ajaxReturn($ajaxReturn);
        }
        if(empty($ch_id)){
            $ajaxReturn['code'] = 4;
            $ajaxReturn['msg'] = "缺少渠道id";
            $this->ajaxReturn($ajaxReturn);
        }
        if(empty($price)){
            $ajaxReturn['code'] = 5;
            $ajaxReturn['msg'] = "缺少要修改的金额";
            $this->ajaxReturn($ajaxReturn);
        }
    	$uid = explode(',',$id);
        $uch_id = explode(',',$ch_id);
        $uprice = explode(',',$price);
        $data['sign'] = $state;
        $data['settlement_person'] = $name;
        $data['settlement_time'] = time();
        foreach($uid as $key=>$v){
        $table = M('channel_withdrawals')->where("id=$v")->save($data);

        $res = M('channel')->where("id="+$uch_id[$key])->setDec("balance",$uprice[$key]);
        
        }

        if($res){
            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "操作成功！";
        }else{
            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "操作失败！";
        }

        $this->ajaxReturn($ajaxReturn);
  	}

    //广告主删除
    public function advertisers_delete(){

        $id = I('get.id');
        $arr = explode(',',$id);
        $advertisers = M('advertisers_withdrawals');
        $data['state'] = 1;
        foreach($arr as $key=>$v){
            $advertisers->where("id=$v")->save($data);
        }

        $ajaxReturn['code'] = 1;
        $ajaxReturn['msg'] = "删除成功";
        $this->ajaxReturn($ajaxReturn);
    }

    //渠道商删除
    public function channel_delete(){
        $id = I('get.id');
        $arr = explode(',',$id);
        $advertisers = M('channel_withdrawals');
        $data['state'] = 1;

        foreach($arr as $key=>$v){
            $advertisers->where("id=$v")->save($data);
        }

        $ajaxReturn['code'] = 1;
        $ajaxReturn['msg'] = "删除成功";
        $this->ajaxReturn($ajaxReturn);
    }

}
